<?php
namespace heihei\helpers;
use Yii;
use yii\base\UserException;
/**
 * HTTP请求类
 */
class HttpClientHelper {

  /**
   * HTTP GET请求
   * @param  string  $url 请求的URL
   * @return string|throw
   */
  public static function get($url, $headers = null) {
    $curl = curl_init();
    // curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
    // curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    // curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    // curl_setopt($curl, CURLOPT_URL, $url);


    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if(is_array($headers) && count($headers) > 0){
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($curl, CURLOPT_URL, $url);

    $response = curl_exec($curl);
    $status   = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    if(!$response){
      throw new UserException("请求失败，未知错误！status:" . $status, $status);
    }
    if ($status == 200) {
      return $response;
    } else {
      throw new UserException("请求失败,内容:" . $response, $status);
    }
  }



  /**
   * HTTP POST请求
   * @param  string  $url 请求的URL
   * @param  array  $data 请求的数据
   * @return string|throw
   */
  public static function post($url,$data, $headers = null)
  {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//3.请求方式
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if(is_array($headers) && count($headers) > 0){
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    }
    $response = curl_exec($curl);
    $status   = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    if(!$response){
      throw new UserException("请求失败，未知错误！", $status);
    }
    if ($status == 200) {
      return $response;
    } else {
      throw new UserException("请求失败,内容:" . $response, $status);
    }
  }
    /**
     * HTTP POST请求
     * @param  string  $url 请求的URL
     * @param  array  $data 请求的数据
     * @return string|throw
     */
    public static function syncPost($url,$data, $headers = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//3.请求方式
        curl_setopt($curl,CURLOPT_TIMEOUT,1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if(is_array($headers) && count($headers) > 0){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        $response = curl_exec($curl);
        $status   = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if(!$response){
            throw new UserException("请求失败，未知错误！", $status);
        }
        if ($status == 200) {
            return $response;
        } else {
            throw new UserException("请求失败,内容:" . $response, $status);
        }
    }

  /**
   * HTTP GET请求
   * @param  string  $url 请求的URL
   * @return string|throw
   */
  public static function getJson($url) {
    $headers = [
      'Content-Type: application/json; charset=utf-8'
    ];
    $response = static::get($url, $headers);
    $result = json_decode($response, true);
    if(!$result){
      throw new UserException("解析JSON失败，内容:" . $response, $status);
    }
    return $result;
  }

  /**
   * HTTP POST请求
   * @param  string  $url 请求的URL
   * @param  array  $data 请求的数据
   * @return string|throw
   */
  public static function postJson($url,$data,$headers = [])
  {
    $headers = array_merge($headers,[
      'Content-Type: application/json; charset=utf-8'
    ]);
    $response = static::post($url, $data, $headers);
    $result = json_decode($response, true);
    if(!$result){
      throw new UserException("解析JSON失败，内容:" . $response);
    }
    return $result;
  }

  /**
   * HTTP POST XML请求
   * @param  string  $url 请求的URL
   * @param  array  $data 请求的数据
   * @return string|throw
   */
  public static function postXML($url,$data)
  {
    $headers = [
      'Content-Type: application/xml; charset=utf-8',
      'Content-Length: ' . strlen($data)
    ];
    $response = static::post($url, $data, $headers);
    $result = $response;
    if(!$result){
      throw new UserException("解析JSON失败，内容:" . $response);
    }
    return $result;
  }


    /**
     * @param $url
     * @param $jsonString
     * @return bool|string
     */
    public static function curlPostJson($url, $jsonString)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonString))
        );
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
